package com.cyl.h5.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cyl.h5.domain.form.RegisterForm;
import com.cyl.h5.domain.form.WechatLoginForm;
import com.cyl.h5.domain.vo.H5LoginVO;
import com.cyl.h5.domain.vo.RegisterVO;
import com.cyl.h5.domain.vo.ValidatePhoneVO;
import com.cyl.h5.service.H5MemberService;
import com.cyl.manager.ums.domain.entity.Member;
import com.cyl.manager.ums.domain.vo.MemberVO;
import com.cyl.manager.ums.service.MemberService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginMember;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.framework.web.service.TokenService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/h5")
public class H5MemberController {
	
	private static final Logger LOG = LoggerFactory.getLogger(H5MemberController.class);

    @Autowired
    private H5MemberService service;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private MemberService memberService;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public ResponseEntity<RegisterVO> register(@RequestBody RegisterForm request){
        return ResponseEntity.ok(service.register(request));
    }
    @ApiOperation("会员手机号解密")
    @GetMapping("/ums/member/phone")
    public ResponseEntity<String> getMemberPhone(Long spreaduid){
    	Member member = memberService.selectById(spreaduid);
    	if(member!=null && member.getPhoneEncrypted()!=null) {
            return ResponseEntity.ok(memberService.getPhoneDecrypted(member.getPhoneEncrypted()));
    	}
        return ResponseEntity.ok("");
    }

    @ApiOperation("注册登录验证码校验手机号")
    @GetMapping("/validate/{phone}")
    public ResponseEntity<ValidatePhoneVO> validate(@PathVariable String phone){
        return ResponseEntity.ok(service.validate(phone));
    }

    @ApiOperation("手机号密码登录")
    @PostMapping("/account/login")
    public ResponseEntity<H5LoginVO> accountLogin(@RequestBody String data){
        return ResponseEntity.ok(service.accountLogin(data));
    }

    @PostMapping("/wechat/login")
    public ResponseEntity<H5LoginVO> wechatLogin(@RequestBody WechatLoginForm form) throws Exception {
        return ResponseEntity.ok(service.wechatLoginByOpenId(form));
    }

    @ApiOperation("sms登录")
    @PostMapping("/sms/login")
    public ResponseEntity<H5LoginVO> smsLogin(@RequestBody String data){
        return ResponseEntity.ok(service.smsLogin(data));
    }

    @ApiOperation("获取会员信息")
    @GetMapping("/member/info")
    public ResponseEntity<MemberVO> getMemberInfo(){
        return ResponseEntity.ok(service.getMemberInfo());
    }
    
    
    @ApiOperation("修改会员基本信息")
    @PostMapping("/member/updateJbxx")
    public ResponseEntity<Integer> updateJbxx(@RequestBody Member member){
        Member memberDb = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        if(memberDb==null || memberDb.getId()==null) {
            return ResponseEntity.ok(0);
        }
        member.setId(memberDb.getId());
        return ResponseEntity.ok(service.updateJbxx(member,false));
    }
    
    
    
    @ApiOperation("绑定店铺信息")
    @PostMapping("/member/binding")
    public ResponseEntity<Boolean> binding(@RequestBody Member member){
    	Long deptDpId = member.getDeptDpId();
    	Long spreaduid  = member.getSpreaduid();
    	LOG.error("绑定店铺信息deptDpId["+deptDpId+"]");
    	LOG.error("绑定群主信息spreaduid["+spreaduid+"]");
    	service.binding(deptDpId,spreaduid);
        return ResponseEntity.ok(true);
    }
    

    @ApiOperation("设置会员微信信息")
    @PostMapping("/member/setWechatInfo")
    public void setWechatInfo(@RequestBody String data){
        service.setWechatInfo(data);
    }

    @ApiOperation("新增会员登录记录")
    @GetMapping("/record/login")
    public void add(HttpServletRequest request) {
        LoginMember loginMember = tokenService.getLoginMember(request);
        if (loginMember != null){
            service.insert(loginMember.getMemberId());
        }
    }
}
